package cn.xzqwjw.taskmanager.service.impl;

import cn.xzqwjw.taskmanager.common.customEnum.ResponseCodeEnum;
import cn.xzqwjw.taskmanager.common.exception.CustomException;
import cn.xzqwjw.taskmanager.domain.pojo.SysAdminRole;
import cn.xzqwjw.taskmanager.domain.vo.ResponseVo;
import cn.xzqwjw.taskmanager.repository.SysAdminRoleMapper;
import cn.xzqwjw.taskmanager.service.SysAdminRoleService;
import cn.xzqwjw.taskmanager.utils.CommonUtil;
import cn.xzqwjw.taskmanager.utils.ConvertUtil;
import cn.xzqwjw.taskmanager.utils.JsonUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author rush
 */
@Service("SysAdminRoleService")
@Transactional(rollbackFor = Exception.class)
public class SysAdminRoleServiceImpl extends ServiceImpl<SysAdminRoleMapper, SysAdminRole>
    implements SysAdminRoleService {

  /**
   * 根据角色id修改此角色所属的所有管理员
   */
  @Override
  public String modifyAdminIdsByIdRole(Integer idRole, String paramsJson) {
    CommonUtil.checkNull(idRole, paramsJson);
    Map<?, ?> paramsMap = JsonUtil.json2Map(paramsJson);
    if (!paramsMap.containsKey("AdminIds")) {
      throw new CustomException(ResponseCodeEnum.BAD_REQUEST);
    }
    Object obj = paramsMap.get("AdminIds");
    CommonUtil.checkNull(obj);
    List<Integer> adminIdList = ConvertUtil.obj2List(obj, Integer.class);
    CommonUtil.checkNull(adminIdList);
    addAdminIdsByRoleId(idRole, adminIdList);
    return JsonUtil.obj2Json(ResponseVo.success(ResponseCodeEnum.SUCCESS));

  }

  /**
   * 根据角色id得到此角色拥有的所有管理员的id
   */
  @Override
  public String listAdminIdByIdRole(Integer idRole) {
    if (idRole <= 0) {
      throw new CustomException(ResponseCodeEnum.BAD_REQUEST);
    }
    QueryWrapper<SysAdminRole> wrapper = new QueryWrapper<>();
    wrapper.eq("id_role", idRole);
    List<SysAdminRole> adminRoleList = list(wrapper);
    // 将所有的管理员id放到数组里
    List<Integer> adminIdList = new ArrayList<>();
    for (SysAdminRole sysAdminRole : adminRoleList) {
      adminIdList.add(sysAdminRole.getIdAdmin());
    }
    return JsonUtil.obj2Json(ResponseVo.success(adminIdList.toArray()));
  }

  private void addAdminIdsByRoleId(int roleId, List<Integer> adminIdList) {
    // 1、先根据roleId把此角色所属的已有管理员都删除
    QueryWrapper<SysAdminRole> wrapper = new QueryWrapper<>();
    wrapper.eq("id_role", roleId).gt("id", 0);
    remove(wrapper);
    // 2、批量向角色权限表里添加记录
    // 如果 adminIdList 为空，说明是清空某角色的所有权限，直接返回即可
    if (CollectionUtils.isEmpty(adminIdList)) {
      return;
    }
    List<SysAdminRole> listAdminRole = new ArrayList<>();
    SysAdminRole adminRole;
    for (int idAdmin : adminIdList) {
      adminRole = new SysAdminRole();
      adminRole.setIdRole(roleId);
      adminRole.setIdAdmin(idAdmin);
      listAdminRole.add(adminRole);
    }
    if (!saveBatch(listAdminRole)) {
      throw new CustomException(ResponseCodeEnum.ERROR_INSERT);
    }
  }

}
